/**
 * @Author: dn-jinmin
 * @File:  tlog
 * @Version: 1.0.0
 * @Date: 2024/3/7
 * @Description:
 */

package tlog

import (
	"crypto/rand"
	"fmt"
	"strconv"
	"strings"

	"gitee.com/dn-jinmin/gen-id/utils"
)

const idLen = 8

func RandId() string {
	b := make([]byte, idLen)
	_, err := rand.Read(b)
	if err != nil {
		return Randn(idLen)
	}

	return fmt.Sprintf("%x%x%x%x", b[0:2], b[2:4], b[4:6], b[6:8])
}

func Randn(n int) string {
	n = n * 2

	var ret strings.Builder
	ret.Grow(n)
	utils.RandIntHandler(10, n, func(num, i int) {
		ret.Write([]byte(strconv.Itoa(num)))
	})
	return ret.String()
}
